package top.fkxuexi.blogapi.core.config.shiro;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SaltedAuthenticationInfo;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;

import org.apache.shiro.util.ByteSource;
import top.fkxuexi.blogapi.core.utils.EncryptUtils;

import java.util.Base64;
import java.util.concurrent.atomic.AtomicInteger;

public class CustomerCredentialsMatcher extends HashedCredentialsMatcher{


    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {

        String originalPwd = new String((char[]) token.getCredentials());
        String encryptPwd = (String) info.getCredentials();

        int encryptTimes = getHashIterations();
        ByteSource salt ;
        String encrypt ;

        if (info instanceof SaltedAuthenticationInfo){
            SaltedAuthenticationInfo sInfo = (SaltedAuthenticationInfo) info;
            salt = sInfo.getCredentialsSalt();
             encrypt = EncryptUtils.encrypt(originalPwd, encryptTimes, salt, EncryptUtils.EncryptType.MDG);

        }else{
            encrypt =  EncryptUtils.encrypt(originalPwd,encryptTimes,EncryptUtils.EncryptType.MDG);
        }
        return encryptPwd.equals(encrypt);
    }
}
